package com.xiyu.service.repository.restaurant.recipe;

import com.xiyu.service.model.restaurant.recipe.*;
import com.xiyu.service.vo.restaurant.recipe.RecipePageQueryDetailInput;
import com.xiyu.service.model.restaurant.dish.RestaurantDishFetcher;
import com.xiyu.service.vo.restaurant.recipe.RecipeQueryInput;
import org.babyfish.jimmer.spring.repository.JRepository;
import org.babyfish.jimmer.Page;
import org.springframework.util.StringUtils;

import java.util.Optional;

public interface RestaurantRecipeRepository extends JRepository<RestaurantRecipe, Long> {
    RestaurantRecipeTable restaurantRecipeTable = RestaurantRecipeTable.$;

    default Page<RestaurantRecipe> pageQuery(RecipeQueryInput inputVO){
        return sql().createQuery(restaurantRecipeTable)
                .whereIf(StringUtils.hasText(inputVO.getRecipeName()), () -> restaurantRecipeTable.recipeName().like(inputVO.getRecipeName()))
                .whereIf(inputVO.getRecipeType() != null, restaurantRecipeTable.recipeType().eq(inputVO.getRecipeType()))
                .whereIf(inputVO.getComboId() != null, restaurantRecipeTable.comboId().eq(inputVO.getComboId()))
                .orderBy(restaurantRecipeTable.id().desc())
                .select(restaurantRecipeTable.fetch(RestaurantRecipeFetcher.$.allTableFields()
                        .combo(RestaurantComboFetcher.$.allTableFields())))
                .fetchPage(inputVO.getPageNo() - 1, inputVO.getPageSize());
    }

    default Optional<RestaurantRecipe> findDetailById(Long id){
        return sql().createQuery(restaurantRecipeTable)
                .where(restaurantRecipeTable.id().eq(id))
                .select(restaurantRecipeTable.fetch(RestaurantRecipeFetcher.$.allTableFields().
                        details(RestaurantRecipeDetailFetcher.$.allTableFields()
                                .dish(RestaurantDishFetcher.$.allTableFields()))))
                .fetchOptional();
    }

    Optional<RestaurantRecipe> findFirstByRecipeName(String recipeName);

    Optional<RestaurantRecipe> findFirstByRecipeNameAndIdNot(String recipeName, Long id);

    default Page<RestaurantRecipe> pageQueryDetail(RecipePageQueryDetailInput inputVO){
        return sql().createQuery(restaurantRecipeTable)
                .whereIf(inputVO.getComboId() != null, restaurantRecipeTable.comboId().eq(inputVO.getComboId()))
                .whereIf(inputVO.getRecipeType() != null, restaurantRecipeTable.recipeType().eq(inputVO.getRecipeType()))
                .whereIf(StringUtils.hasText(inputVO.getRecipeName()), () -> restaurantRecipeTable.recipeName().like(inputVO.getRecipeName()))
                .orderBy(restaurantRecipeTable.id().desc())
                .select(restaurantRecipeTable.fetch(RestaurantRecipeFetcher.$.allTableFields()
                        .details(RestaurantRecipeDetailFetcher.$.allTableFields()
                                .dish(RestaurantDishFetcher.$.allTableFields()))))
                .fetchPage(inputVO.getPageNo() - 1, inputVO.getPageSize());
    }


}
